package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpSocBackResponsibleDAO;
import org.jsola.hr.dto.EmpSocBackResponsibleAddDTO;
import org.jsola.hr.dto.EmpSocBackResponsibleUpdateDTO;
import org.jsola.hr.dto.SetSocBackResponsibleDTO;
import org.jsola.hr.entity.EmpSocBackResponsibleDO;
import org.jsola.hr.query.EmpSocBackResponsibleQuery;
import org.jsola.hr.service.IEmpSocBackResponsibleService;
import org.jsola.hr.vo.EmpSocBackResponsibleListVO;
import org.jsola.hr.vo.EmpSocBackResponsibleVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 社保回款负责人信息
 *
 * @author zhr
 */
@Service("hrEmpSocBackResponsibleServiceImpl")
public class EmpSocBackResponsibleServiceImpl implements IEmpSocBackResponsibleService {

    @Autowired
    private IEmpSocBackResponsibleDAO empSocBackResponsibleDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpSocBackResponsibleVO save(EmpSocBackResponsibleAddDTO empSocBackResponsibleAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpSocBackResponsibleDO empSocBackResponsibleDO = empSocBackResponsibleAddDTO.to(EmpSocBackResponsibleDO.class);
        // 保存
        empSocBackResponsibleDO = save(empSocBackResponsibleDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empSocBackResponsibleDO.to(EmpSocBackResponsibleVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpSocBackResponsibleUpdateDTO empSocBackResponsibleUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpSocBackResponsibleDO empSocBackResponsibleDO = empSocBackResponsibleUpdateDTO.to(EmpSocBackResponsibleDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empSocBackResponsibleDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empSocBackResponsibleIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empSocBackResponsibleIds);
    }


    @Override
    public EmpSocBackResponsibleVO selectById(String empSocBackResponsibleId, String siteId) {
        EmpSocBackResponsibleDO empSocBackResponsibleDO = selectDOById(empSocBackResponsibleId, siteId);
        if (empSocBackResponsibleDO == null) {
            return null;
        }
        return empSocBackResponsibleDO.to(EmpSocBackResponsibleVO.class);
    }

    @Override
    public List<EmpSocBackResponsibleListVO> select(EmpSocBackResponsibleQuery empSocBackResponsibleQuery, String siteId) {
        List<EmpSocBackResponsibleDO> empSocBackResponsibleDOList = selectDO(empSocBackResponsibleQuery, siteId);
        if (CollectionUtils.isEmpty(empSocBackResponsibleDOList)) {
            return empSocBackResponsibleDOList == null ? null : new ArrayList<>();
        }
        return empSocBackResponsibleDOList.stream()
                .map(empSocBackResponsibleDO -> empSocBackResponsibleDO.to(EmpSocBackResponsibleListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpSocBackResponsibleQuery empSocBackResponsibleQuery, String siteId) {
        Example example = buildExample(empSocBackResponsibleQuery, siteId);
        return empSocBackResponsibleDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpSocBackResponsibleListVO> selectPage(EmpSocBackResponsibleQuery empSocBackResponsibleQuery, String siteId) {
        Example example = buildExample(empSocBackResponsibleQuery, siteId);
        Page<EmpSocBackResponsibleDO> page = empSocBackResponsibleDAO.selectPageByExample(example,
                empSocBackResponsibleQuery.getPageNo(),
                empSocBackResponsibleQuery.getPageSize());

        return page.to(EmpSocBackResponsibleListVO.class);
    }

    @Override
    public EmpSocBackResponsibleDO selectDOById(String empSocBackResponsibleId, String siteId) {
        return listById(empSocBackResponsibleId, siteId);
    }

    @Override
    public List<EmpSocBackResponsibleDO> selectDO(EmpSocBackResponsibleQuery empSocBackResponsibleQuery, String siteId) {
        Example example = buildExample(empSocBackResponsibleQuery, siteId);
        return empSocBackResponsibleDAO.selectByExample(example);
    }

    @Override
    public void settingsResponsible(SetSocBackResponsibleDTO settingsDTO, TokenUser tokenUser) {
        ParamException.notEmpty(settingsDTO.getEmpIdList(), "请选择回款人");
        Example example = new Example(EmpSocBackResponsibleDO.class);
        example.and()
                .andIn("empId", settingsDTO.getEmpIdList());
        List<EmpSocBackResponsibleDO> empSocBackResponsibleList = empSocBackResponsibleDAO.selectByExample(example);
        List<String> selectEmpList = empSocBackResponsibleList.stream()
                .map(EmpSocBackResponsibleDO::getEmpId).collect(Collectors.toList());
        List<String> updateId = new ArrayList<>();
        List<EmpSocBackResponsibleDO> insertList = new ArrayList<>();
        // 筛选出来新添加的和需要更改的
        for (String empId : settingsDTO.getEmpIdList()) {
            if (selectEmpList.contains(empId)) {
                updateId.add(empId);
            } else {
                EmpSocBackResponsibleDO insert = new EmpSocBackResponsibleDO();
                insert.setEmpId(empId);
                insert.setEmpResponsibleId(settingsDTO.getResponsibleId());
                insert.setSiteId(tokenUser.getSiteId());
                insert.preInsert(tokenUser.getUserId());
                insertList.add(insert);
            }
        }
        if(!CollectionUtils.isEmpty(updateId)){
            // 需要更改的
            if(StringUtils.isEmpty(settingsDTO.getResponsibleId())){
                deleteByEmpId(updateId);
            } else {
                EmpSocBackResponsibleDO updateDO = new EmpSocBackResponsibleDO();
                updateDO.setEmpResponsibleId(settingsDTO.getResponsibleId());
                example.clear();
                example.and().andIn("empId", updateId);
                empSocBackResponsibleDAO.updateByExampleSelective(updateDO, example);
            }
        }
        if(!CollectionUtils.isEmpty(insertList)){
            // 新添加的
            empSocBackResponsibleDAO.insertListAndSetId(insertList);
        }
    }

    @Override
    public void deleteByUserId(List<String> userIds) {
        if(!CollectionUtils.isEmpty(userIds)){
            Example example = new Example(EmpSocBackResponsibleDO.class);
            example.and().andIn("empResponsibleId",userIds);
            empSocBackResponsibleDAO.deleteByExample(example);
        }
    }

    /**
     * 清除对应员工的负责人
     */
    private void deleteByEmpId(List<String> deleteEmpId){
        if(!CollectionUtils.isEmpty(deleteEmpId)){
            Example example = new Example(EmpSocBackResponsibleDO.class);
            example.and().andIn("empId",deleteEmpId);
            empSocBackResponsibleDAO.deleteByExample(example);
        }

    }

    /**
     * 根据查询参数，构建example
     *
     * @param empSocBackResponsibleQuery 查询参数
     * @param siteId                     所属站点id
     * @return example
     */
    private Example buildExample(EmpSocBackResponsibleQuery empSocBackResponsibleQuery, String siteId) {
        Example example = new Example(EmpSocBackResponsibleDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, empSocBackResponsibleQuery.getOrders());
        return example;
    }
}




